/*
 *  WsvJdbc - a JDBC driver for WSV (values separated by whatever) data files
 *  Copyright (C) 2004  Daniele Pes
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


package axs.jdbc.utils;

import java.lang.reflect.Array;


	/**
	 * This class is used to provide array utilities for the WsvJdbc driver.
	 * 
	 * @author 	Daniele Pes
	 * @author  Pier Ribatto
	 * @author  Marco Pozzato
	 * @author  Marco Morelli
	 * @author  Fabio Pessina  
	 * @version    $Id: ArrayUtilities.java,v 1.2 2007/04/16 17:24:37 montag451 Exp $
	 */
	public class ArrayUtilities {


	public static int howManyNotNull(Object array){				
		int    numOfLocations;
		int 	 result;
		Object arrayEl;
		//
		numOfLocations = Array.getLength(array);
		result 			= numOfLocations;
		for(int i=0; i<numOfLocations; i++){
			arrayEl = Array.get(array, i);
			if (arrayEl == null){
				result--;
			}				
		}
		return result;
	}


	/**
	 * Print an array with standard separator (\n).
	 */
	public static String printArray(Object array) {
	   return printArray(array, "\n");
	}


	/**
	 * Print an array with the specified separator.
	 */
	public static String printArray(Object array, 
										     String sep) {
	   return printArray(array, "[", sep, "]");
	}


	public static String printArray(Object       array, 
								           StringBuffer buf, 
								           String 		sep) {
	   return printArray(array, buf, "[", sep, "]").toString();      
	}


	public static String printArray(Object array, 
	                                String initial, 
	                                String sep, 
	                                String end){
	   StringBuffer buf = new StringBuffer();
	   buf.append(initial);
	   printArray(array, buf, initial, sep, end);
	   buf.append(end);
	   return buf.toString();
	}


	public static StringBuffer printArray(Object 		array, 
	                                      StringBuffer buf, 
	                                      String 		initial,
	                                      String 		sep,
	                                      String 		end)
	   throws IllegalArgumentException, ArrayIndexOutOfBoundsException {      	
	   // This will throw an exception if array is not an array
	   if(array == null)
	   	return new StringBuffer("");	   	   		  
	   int length = Array.getLength(array);
	   for (int i = 0; i < length; i++) {
	      if (i != 0) buf.append(sep);
	      // If the array is primitive will return wrapper object
	      Object obj = Array.get(array, i);
	      if (obj != null) {
	         if (obj.getClass().isArray()) {
	            buf.append(ArrayUtilities.printArray(obj, initial, sep, end));
	         } else {
	            buf.append(obj.toString());
	         }
	      } else {
	         buf.append("null");
	      }
	   }
	   return buf;
	}


	public static int howManyNotNull(Object[] array){     
	   if(array == null) return 0;
	   int result = 0;
	   for(int i=0; i<array.length; i++){
	      if(array[i] != null) result++;
	   }
	   return result;
	}
}
